home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE3 / POWERBASE / DOCS / Ch06-Calcs < prev    next >
Text File  |  2001-01-31  |  13KB  |  268 lines

  1. ==============================
  2. Ch 6 − Performing Calculations
  3. ==============================
  4.  
  5. There are two mechanisms for performing calculations in Powerbase:
  6.  
  7. •       Self-calculating fields which automatically calculate and display a
  8.         value derived from other fields. These belong to the field class
  9.         called Computed (see 4.2.9). 
  10.  
  11. •       Arithmetic performed on a column in a report. 
  12.  
  13. Computed fields may be of Calculated or Composite types. Calculated fields
  14. display a numeric result derived from other fields in the record. When the
  15. mouse pointer is moved over such a field it changes into a shape resembling
  16. a pocket calculator. Composite fields perform a similar function but display
  17. the result as a character string instead of a number. In this case the mouse
  18. pointer changes into a large “+” sign. The latter do not, strictly, involve
  19. “calculations” at all but because Composite and Calculated fields work in
  20. the same way it is convenient to deal with them together. It isn’t possible
  21. to type data directly into either of these special types of field.
  22.  
  23. There are also Computed fields for the same types of data as Stamp fields of
  24. types (c), (d), (e), (f), (g) and (h) (see 4.2.10). They have identical
  25. names but, whereas Stamp fields are entered when the record is created and
  26. don’t change thereafter, the Computed types update whenever a record is
  27. displayed
  28.  
  29. It is also possible to write your own functions in Basic and use them in
  30. Calculated and Composite fields. The system variable TIME$ is useful in this
  31. connection. 
  32.  
  33. 6.1     Calculated fields
  34. -------------------------
  35. 6.1.1   Simple calculations using Numeric fields
  36.  
  37. Our first example will be to make a field containing a v.a.t. exclusive
  38. price determine the contents of another field which includes v.a.t. at 17%. 
  39. Let the tags of these fields be VEX and VINC respectively. Click MENU over
  40. VINC to bring up the Field submenu and choose  Calculations.  A window
  41. appears with a writable icon which contains:             
  42.  
  43.                                         VINC=
  44.  
  45. Complete the formula so that it reads:  VINC=VEX*1.17
  46.  
  47. and click on OK or type Return. From now on the value in VINC will change
  48. whenever you type something in VEX.  Since the v.a.t. rate is liable to
  49. change it would be better to place the current percentage rate in another
  50. Numeric field called RATE and make the formula attached to VINC read:
  51.  
  52.         VINC=VEX+(VEX*RATE/100)
  53.  
  54. The second example calculates the average of four fields No1, No2, No3 and
  55. No4, placing the result in a Calculated field AVGE.  The required formula
  56. is:
  57.  
  58.         AVGE=(No1+No2+No3+No4)/4 
  59.  
  60. You may also enter a tag into a calculation formula by clicking on the
  61. required field with Ctrl held down or by choosing it from the pop-up menu of
  62. fields available from the calculation window. The tag will be entered in the
  63. formula at the caret position. You are strongly recommended to use one or
  64. other of these methods since Powerbase finds it very difficult to decide
  65. whether or not a formula is valid and errors are usually detected only when
  66. the calculation is actually attempted.
  67.  
  68. 6.1.2   Making calculations retrospective
  69.  
  70. The default setting is such that entering or altering a formula affects only
  71. those records added or altered after the formula entry/change. The changes
  72. can be made retrospective by selecting the Recalculate existing records
  73. switch on the formula entry window. On clicking OK you will be asked to
  74. confirm that you want previous records to be made consistent with the
  75. formula you have just entered. Changes affect the current subfile only, but
  76. can be easily implemented in other subfiles by changing to the required
  77. subfile, calling up the formula entry window, selecting the option button
  78. and clicking OK.  
  79.  
  80. The Preferences window, accessible from the iconbar menu, contains a switch
  81. labelled Recalculate on opening. If this is selected then calculations
  82. involving the system variable TIME$ will be updated automatically for all
  83. records when the database is opened (see also 6.3).  This could be needed
  84. where calculations involve dates, ages, times etc. which might change from
  85. one work session to the next even though no editing of the data has been
  86. done.
  87.  
  88.                                 - 40 -
  89.  
  90. 6.1.3   Calculations using non-numeric fields (!)
  91.  
  92. This isn’t as daft as it looks! You might think that the process described
  93. in 6.1.1 is only applicable to numeric fields, but a non-numeric field may
  94. be specified in the formula − if it is linked to a validation table. To be
  95. of use there must be  numeric data in the column of the validation table
  96. immediately following the one to which the field is linked (see 5.2).
  97. Suppose, for example, we have fields in a student record for A-level exam
  98. grades, the field tags being GR1, GR2, GR3, GR4. The grades are non-numeric
  99. but they map onto the numeric points system which universities use to
  100. control entry. The relationship of grades to points is as shown below.
  101.  
  102. Grade   Points
  103. =====   ======
  104. A       10   
  105. B        8    
  106. C        6    
  107. D        4     
  108. E        2 
  109.  
  110. A validation table could be set up with the grades in column 0 and the
  111. equivalent points in column 1. Each of fields GR1−GR4 would be linked to
  112. column 0 thus restricting input to the capital letters A-E. A further field
  113. of Calculated type would be created to hold the points score. If we
  114. associate this field (PTS) with the formula:
  115.  
  116. PTS=GR1+GR2+GR3+GR4 
  117.  
  118. entering or changing the letter grades in GR1-GR4 will cause the correct
  119. points score to appear in PTS.
  120.  
  121. 6.1.4   Calculations involving times
  122.  
  123. There is a field type Time which will only accept valid 24-hour times in
  124. hh:mm:ss format.  If the tag of such a field is included in a calculation
  125. formula it will be converted into seconds and the result used in the
  126. calculation. Thus a Calculated field DIFF could use two Time fields, TIM1
  127. and TIM2, in the formula:
  128.  
  129. DIFF=TIM1-TIM2
  130.  
  131. and Powerbase would keep DIFF updated to show the difference in seconds
  132. between the two times.  Three times could be averaged and the result (in
  133. seconds) placed in a field AVGE using the formula:
  134.  
  135. AVGE=(TIM1+TIM2+TIM3)/3
  136.  
  137. 6.1.5   Calculations involving dates
  138.  
  139. You may also include date fields in a calculation in a similar manner to the
  140. use of Time fields (see 6.1.4). A Calculated field DIFF could be used to
  141. show the number of days between two dates using the formula:
  142.  
  143. DIFF=DAT1-DAT2
  144.  
  145. 6.2     Composite fields
  146. ------------------------
  147. To enter the formula for a Composite field follow the same procedure as for
  148. a Calculated field. Note that the relevant entry on the Field submenu now
  149. says Combine fields. Formula entry is similar to that for Calculated fields.
  150. The result of the “calculation” is a character string and is usually result
  151. of string operations. One of the most frequently used will be “+” which
  152. allows fields to be joined together.  Suppose your database contains fields
  153. for surname (SNAM) and forename (FNAM) and you want to be able to print
  154. names in the format forename-surname. Define a Composite field called NAME
  155. and attach to it the formula:
  156.  
  157. NAME=FNAM+“ ”+SNAM
  158.  
  159. Note the quoted space separating the names.  You might want the NAME field
  160. to show only an initial plus the surname. This could be extracted using the
  161. Basic function LEFT$ * to produce the formula:
  162.  
  163. NAME=LEFT$(FNAM,1)+“. ”+SNAM
  164.  
  165. Composite fields may be used in conjunction with Time fields to perform
  166. genuine “clock arithmetic”, e.g. referring to the examples in 6.1.4, if we
  167. attached the formulae to Composite fields instead of Calculated fields they
  168. would display the difference and average respectively in hh:mm:ss format
  169. instead of in seconds. 
  170.  
  171.         As with Calculated fields updating occurs after editing a field
  172. whose tag appears in the attached formula. Thus NAME would be updated after
  173. changes to FNAM or SNAM and DIFF after changes to TIM1 or TIM2. You can,
  174. however, have Composite fields which make use of the Basic system variable
  175. TIME$. Thus a field DAY could be linked to the formula:
  176.  
  177. DAY=LEFT$(TIME$,3) 
  178.  
  179. to make it show today as Mon, Tue etc. No field tags are referred to in the
  180. formula so DAY gets updated immediately before displaying the record so that
  181. the information is correct at that time. 
  182.  
  183.                                 - 41 -
  184.  
  185. 6.3     User functions
  186. ----------------------
  187. These are functions, written in Basic, which accept field tags as parameters
  188. and can be included in the calculation formulae of self-calculating fields.
  189. All such functions must return only their principal value: RETURN variables
  190. in the parameter list aren’t allowed. The name of each function must begin
  191. with an upper-case “U”, e.g. FNUaverage. This avoids duplication of
  192. function-names which occur in Powerbase. 
  193.  
  194.         All the user functions to be used by a database must be included in
  195. a Basic program called UserFuncs which is stored in the database directory
  196. (not in the !Powerbase directory). When the database is opened UserFuncs
  197. will be loaded as a library and Powerbase can call the user functions just
  198. as readily as its own functions. When a user function appears in a
  199. calculation formula the tags of any fields on which the function operates
  200. are included as parameters to the function. Editing any such field makes the
  201. Computed field update.
  202.  
  203.         The distribution disc includes a UserFuncs file containing two
  204. examples of user functions kindly submitted by David Lenthall. Users are
  205. invited to submit other user functions for possible inclusion in the
  206. function library. FNUnow operates on the value of TIME$ from the real-time
  207. clock and returns a string containing the current date in form DD-MM-YY. A
  208. record design could include a field of Composite type, at least eight
  209. characters long and tagged as, for example, DATE. Associating the field with
  210. the following formula would cause the field to display the current date at
  211. all times:
  212.  
  213. DATE=FNUnow(TIME$) 
  214.  
  215. If the function always operates on TIME$ and never on any other string, why
  216. do we bother including TIME$ as a parameter to the function?  A Calculated
  217. or Composite field is normally only updated when you edit a field on which
  218. its value depends.  The field DATE in the above example doesn’t depend on
  219. any other fields and would therefore never be updated!  The inclusion of
  220. TIME$ causes the Composite field to be updated immediately before the record
  221. is displayed.  The same trick of using TIME$ as a parameter, even if the
  222. function makes no use of it at all, can be used for any user function
  223. associated with a field which you want updating without having to edit the
  224. record, but note that updating will only occur if you call up the record for
  225. display. See 14.4.3 for how to make all records in the current subfile
  226. update on opening the database.
  227.  
  228.         The second function, FNUageinyrs accepts two parameters, each of
  229. which should a date in DD-MM-YY format, and returns the difference between
  230. the dates to the nearest year. The first date should be the earlier of the
  231. two. If your record design has a field for Date of Birth (let’s call its tag
  232. DOB) you can use this function together with FNUnow (which returns today’s
  233. date in the required format) to make a Composite field (AGE) display a
  234. person’s age in years by using the formula: 
  235.  
  236. AGE=FNUageinyrs(DOB,FNUnow(TIME$)) 
  237.  
  238. Considerable care is needed in constructing user functions as it is very
  239. easy to make Powerbase generate errors. In particular you must avoid giving
  240. a function a name which is the same as a field tag − or even one which
  241. contains a field tag as a substring. The two functions described could not,
  242. for instance, be used in a database which had a field tagged as “now” or
  243. “age”, although “NOW” and “AGE” could be used.  To avoid this problem users
  244. are recommended to form the habit of giving tags names in upper case and
  245. user functions names in lower case (apart from the “U”, of course).
  246.  
  247. 6.4     Calculations on a column of a report
  248. --------------------------------------------
  249. There is an option on the Print submenu called Numeric fields which is
  250. normally shaded. It only becomes available when you highlight one or more
  251. Numeric or Calculated fields for printing. You can then access a window
  252. listing all the fields (if any) in the record which are of these two types.
  253. Associated with each field are six check-boxes which can be selected to 
  254. include the following in the report:
  255.  
  256. •       Count (number of values in column)
  257. •       Sum
  258. •       Average
  259. •       Standard deviation
  260. •       Maximum value in column 
  261. •       Minimum value in column            
  262.  
  263. The check-boxes are shaded until a field is included in a print selection.
  264. They then become “live” and any or all of them may be chosen by clicking
  265. with SELECT. The information requested is then added to the report footer
  266. when printing takes place.
  267.  
  268.                                 - 42 -